package pl.edu.agh.io.gpstracker.dao;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

/**
 * Podstawowe operacje na bazie danych.
 */
public abstract class GenericDaoImpl<E, ID> extends CustomHibernateDaoSupport
		implements GenericDao<E, ID> {

	private Class<E> entityType;

	public GenericDaoImpl(Class<E> entityType) {
		this.entityType = entityType;
	}

	@Override
	public void count() {

	}

	@Override
	public void delete(E entity) {
		getHibernateTemplate().delete(entity);

	}

	@Override
	
	public void saveOrUpdate(E entity) {
		getHibernateTemplate().saveOrUpdate(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<E> findAll() {
		return (List<E>) getHibernateTemplate().loadAll(entityType);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public E findById(Integer id) {
		return (E) getHibernateTemplate().find("from " + entityType.getClass() +" where id = ?", new Object[]{id}).iterator().next();
	}

}
